<template>
  <el-form
      label-position="top"
      label-width="100px"
      :model="user"
      class="login-background"
      :style="background"
      :rules="rules"
  >
    <el-row class="login-header">
        <span class="login-log">
        <img
            :width="200"
            src="@/assets/logo.png"
        />
          </span>
    </el-row>
    <el-tabs type="border-card" v-model:activeKey="activeKey" class="login-main">
      <el-tab-pane key="1" label="免密码登陆">
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="user.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="验证码" prop="verificationCode">
          <el-row :gutter="20">
            <el-col span="22">
              <el-input v-model="user.verificationCode" type="text"></el-input>
            </el-col>
            <el-col span="2" :style="{'position':'absolute','right':'0'}">
              <el-button type="text" @click.prevent="sendCode(0)">短信验证</el-button>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item :style="{'margin-top':'3%'}">
          <el-button
              :style="{'width':'100%'}"
              type="primary"
              html-type="submit"
              @click.prevent="login(false)"
          >
            注册/登陆
          </el-button>
        </el-form-item>
      </el-tab-pane>

      <el-tab-pane key="2" label="密码登陆">
        <el-form-item v-if="isPhoneLogin" label="手机号" prop="phone">
          <el-input v-model="user.phone" autocomplete="off" type="text"></el-input>
        </el-form-item>
        <el-form-item v-else label="邮箱" prop="email">
          <el-input v-model="user.email" autocomplete="off" type="text"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="user.password" type="password"></el-input>
        </el-form-item>
        <el-form-item :style="{'height':'32px','line-height':'32px'}">
          <el-button v-if="isPhoneLogin" type="text" @click="changeLoginType(1)">邮箱登陆
          </el-button>
          <el-button v-else type="text" @click="changeLoginType(0)">手机登陆</el-button>
          <span :style="{'float':'right'}">
            <el-button type="text">忘记密码</el-button>
          </span>
        </el-form-item>
        <el-form-item :style="{'margin-top':'3%'}">
          <el-button
              :style="{'width':'100%'}"
              type="primary"
              html-type="submit"
              @click.prevent="login(true)"
          >
            登陆
          </el-button>
        </el-form-item>
      </el-tab-pane>
    </el-tabs>
    <el-row :style="{'color':'#606266','justify-content':'center','margin-top':'1%'}">
      <span :style="{'color':'var(--el-color-danger)'}">*</span>
      <small>若为初次登陆,则会默认生成随机密码,请登陆后到个人中心修改密码</small>
    </el-row>
  </el-form>
</template>

<script>
import { defineComponent, reactive, ref } from 'vue'
import { checkPassword, checkEmail, checkUsername, checkPhone, checkVerificationCode } from '../utils/validator'
import { doLogin, sendVerificationCode } from '@/api/user'
import { ElMessage } from 'element-plus'
import router from '../router'

export default defineComponent({
  name: 'Login',
  components: {},
  setup () {
    const user = reactive({
      username: '',
      password: '',
      phone: '',
      verificationCode: '',
      email: '',
      loginType: 0,
      havePassword: 0
    })
    const activeKey = ref('')
    const autoLogin = ref(false)
    const isPhoneLogin = ref(true)
    const changeLoginType = (loginType) => {
      if (loginType === 1) {
        isPhoneLogin.value = false
      } else {
        isPhoneLogin.value = true
      }
      user.loginType = loginType
    }

    const changeHavePassword = (havePassword) => {
      user.loginType = havePassword
    }
    const background = {
      background: 'url(' + require('@/assets/login-background.svg') + ')',
      backgroundRepeat: 'no-repeat',
      backgroundPosition: 'center 110px',
      backgroundSize: '100%'
    }
    const rules = {
      username: [{
        required: true,
        validator: checkUsername,
        trigger: 'change'
      }],
      phone: [{
        required: true,
        validator: checkPhone,
        trigger: 'change'
      }],
      verificationCode: [{
        required: true,
        validator: checkVerificationCode,
        trigger: 'change'
      }],
      email: [{
        required: true,
        validator: checkEmail,
        trigger: 'change'
      }],
      password: [{
        required: true,
        validator: checkPassword,
        trigger: 'change'
      }]
    }
    const login = async (havePassword) => {
      if (user.loginType === 1 && (!user.email || user.email === '')) {
        return
      }
      if (user.loginType === 0 && (!user.phone || user.phone === '')) {
        return
      }
      user.havePassword = havePassword
      const resp = await doLogin({
        phone: user.phone,
        email: user.email,
        password: user.password,
        loginType: user.loginType,
        havePassword: havePassword,
        verificationCode: user.verificationCode
      })
      if (resp.status !== 200 || resp.data.code !== 0) {
        ElMessage({
          message: resp.data.msg,
          type: 'error',
          offset: 90
        })
        return
      }
      ElMessage({
        message: '登陆成功',
        type: 'success',
        offset: 90,
        duration: 1500
      })
      localStorage.setItem('mkcsa-token', resp.data.data.token)
      router.push('/')
    }
    const sendCode = async (sendType) => {
      if (user.phone === '') {
        return
      }
      const resp = await sendVerificationCode({
        sendType: sendType,
        email: user.email,
        phone: user.phone,
        codeType: 2
      })
      if (resp.status === 200 && resp.data.code === 0) {
        ElMessage({
          message: '发送成功',
          type: 'success',
          duration: 1500,
          offset: 90
        })
        return
      }
      ElMessage({
        message: resp.data.msg,
        type: 'error',
        offset: 90
      })
    }
    return {
      user,
      autoLogin,
      activeKey,
      isPhoneLogin,
      login,
      background,
      rules,
      changeHavePassword,
      changeLoginType,
      sendCode
    }
  }
})
</script>

<style scoped>

.login-log {
  width: 60px;
  height: 60px;
  vertical-align: top;

}

.login-title {
  position: relative;
  top: 2px;
  color: rgba(0, 0, 0, .85);
  font-weight: 400;
  font-size: 22px;
  width: 100%;
  text-align: center;
}

.login-log img {
  width: 100%;
}

.login-background {
  height: 100vh;
  padding-top: 30px;
}

.login-header {
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 15px 0;
}

.login-main {
  width: 328px;
  margin: 0 auto;
}
</style>
